package com.qymerge.manage.base.controller;

import com.google.common.reflect.TypeToken;
import com.qymerge.manage.base.enums.ActiveTypes;
import com.qymerge.manage.base.ro.MetaProfitTableTemplateRO;
import com.qymerge.manage.base.ro.MetaProfitTableTemplateRO;
import com.qymerge.manage.base.service.IMetaProfitTableTemplateService;
import com.qymerge.manage.core.bean.*;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.controller.BaseController;
import com.qymerge.manage.core.utils.GsonUtil;
import com.qymerge.manage.core.utils.PrintCodeUtils;
import com.qymerge.manage.core.utils.RequestCheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>Title: IMetaProfitTableTemplateManageController</p>
 * <p>Description: 利润表模板定义表</p>
 *
 * @author Xiao
 * @date 2025/4/29 17:18
 * @version 1.0.0
 */
@Controller
@RequestMapping(value = "base/profitTableTemplate")
public class IMetaProfitTableTemplateManageController extends BaseController {

    @Autowired
    private IMetaProfitTableTemplateService metaProfitTableTemplateService;

    /**
     * 列表
     *
     * @param parameters
     * @param callback
     */
    @RequestMapping("list")
    public void list(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<MetaProfitTableTemplateRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitTableTemplateRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitTableTemplateRO ro = paramValue.getData();

        DataTableRO data = metaProfitTableTemplateService.list(ro, new PageRequest(common.getPage(), common.getSize()));
        PrintCodeUtils.printSuccess(response, data.build(), callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("detail")
    public void detail(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<MetaProfitTableTemplateRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitTableTemplateRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitTableTemplateRO ro = paramValue.getData();

        RequestCheckUtils.checkNotEmpty(ro.getId(), "ID必须");
        MetaProfitTableTemplateRO resultRO = metaProfitTableTemplateService.detail(ro.getId());

        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 详情
     *
     * @param request
     * @param response
     * @param callback
     * @throws Exception
     */
    @RequestMapping("active")
    public void active(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {

        ParamValue<MetaProfitTableTemplateRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitTableTemplateRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitTableTemplateRO ro = paramValue.getData();

        MetaProfitTableTemplateRO resultRO = metaProfitTableTemplateService.active();
        PrintCodeUtils.printSuccess(response, resultRO, callback);
    }

    /**
     * 增加
     *
     * @param request
     * @param response
     * @param callback
     */
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response,

                     @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                     @RequestParam(defaultValue = ParamKey.In.empty) String callback) throws Exception {
        ParamValue<MetaProfitTableTemplateRO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<MetaProfitTableTemplateRO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        MetaProfitTableTemplateRO ro = paramValue.getData();

        ActionResultRO resultRO = metaProfitTableTemplateService.save(ro);
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 删除
     *
     * @param parameters
     * @return
     */
    @RequestMapping(value = "/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response,

                       @RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback) {
        ParamValue<CommonIdsDTO> paramValue = GsonUtil.json2T(parameters, new TypeToken<ParamValue<CommonIdsDTO>>() {
        }.getType());
        ParamCommon common = paramValue.getCommon();
        CommonIdsDTO commonIdsDTO = paramValue.getData();

        ActionResultRO obj = metaProfitTableTemplateService.delete(commonIdsDTO.getIds());
        PrintCodeUtils.print(response, obj, callback);
    }

    /**
     * 上线
     *
     * @param parameters
     * @param callback
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/online")
    public void online(@RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                       @RequestParam(defaultValue = ParamKey.In.empty) String callback,
                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        ParamValue<MetaProfitTableTemplateRO> paramValue = GsonUtil.json2T(parameters, new com.google.gson.reflect.TypeToken<ParamValue<MetaProfitTableTemplateRO>>() {
        }.getType());
        MetaProfitTableTemplateRO ro = paramValue.getData();

        long id = ro.getId();
        RequestCheckUtils.checkNotEmpty(id, "ID必须");
        ActionResultRO resultRO = metaProfitTableTemplateService.updateActive(id, ActiveTypes.OnLine.getStatus(), ActiveTypes.OnLine.getDesc());
        PrintCodeUtils.print(response, resultRO, callback);
    }

    /**
     * 下线
     *
     * @param parameters
     * @param callback
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/offline")
    public void offline(@RequestParam(defaultValue = ParamKey.In.dlt_parameters) String parameters,
                        @RequestParam(defaultValue = ParamKey.In.empty) String callback,
                        HttpServletRequest request, HttpServletResponse response) throws Exception {
        ParamValue<MetaProfitTableTemplateRO> paramValue = GsonUtil.json2T(parameters, new com.google.gson.reflect.TypeToken<ParamValue<MetaProfitTableTemplateRO>>() {
        }.getType());
        MetaProfitTableTemplateRO ro = paramValue.getData();

        long id = ro.getId();
        RequestCheckUtils.checkNotEmpty(id, "ID必须");
        ActionResultRO resultRO = metaProfitTableTemplateService.updateActive(id, ActiveTypes.OffLine.getStatus(), ActiveTypes.OffLine.getDesc());
        PrintCodeUtils.print(response, resultRO, callback);
    }


}
